package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService{
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private DestinationMapper destinationMapper;
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
       List<Region> list =  regionMapper.selectForlist(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(Region region) {
        if(region.getId() == null){
            regionMapper.insert(region);
        }else{
            regionMapper.updateByPrimaryKey(region);
        }
    }

    public void changeHotState(Boolean hot, Long id) {
        regionMapper.changeHotState(hot,id);
    }

    public List<Destination> getDestByRegionId(Long rid) {
        //1.根据地区id查询出ref的值
        Region region = regionMapper.selectByPrimaryKey(rid);
        Long[] refIds = region.getRefIds();
        //根据id去数据库中查询目的地
        return destinationMapper.selectDestByRefids(refIds);
    }

    public void deleteById(Long id) {
        regionMapper.deleteByPrimaryKey(id);
    }

    public List<Region> queryHotRegions() {
        return regionMapper.selectHotRegions();
    }
}
